package issue

import (
	clin "github.com/AnonymousMister/go-gitlab-client/v2/gitlab"
	"github.com/urfave/cli/v2"
	"gitlab-cli/gitlab"
)

var Issue = &cli.Command{
	Name:   "issue",
	Usage:  "操作 issue 关闭 操作",
	Action: action,
	Flags:  flag(),
}

func action(c *cli.Context) error {
	label := c.String("label")
	cl, e := gitlab.GitlabClient(gitlab.Def)
	if e != nil {
		return e
	}
	iqss := &clin.QIssuesRequest{
		Labels: label,
		State:  "opened",
	}
	iqss.PerPage = 20
	for {
		issues, _, e := cl.GetMyIssues(iqss)
		if e != nil {
			return e
		}
		if len(issues) <= 0 {
			break
		}
		up := &clin.UpIssueRequest{
			StateEvent: "close",
		}
		for _, issue := range issues {
			_, _, err := cl.UpIssue(issue.ProjectId, issue.IId, up)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func flag() []cli.Flag {
	flag := []cli.Flag{
		&cli.StringFlag{
			Name:    "label",
			Aliases: []string{"l"},
			Usage:   "过滤标签",
		},
	}
	return flag
}
